/*******************************************************
* REPLICATION CODE for main models in the article in Social Indicators Research:
* Österman, M. 'Can We Trust Education for Fostering Trust? Quasi-experimental Evidence on the Effect of Education and Tracking on Social Trust'
*******************************************************/
* This do-file reproduces the descriptive statistics, regression tables and the figures in the main article.
* The tables are shown as Stata outpuit and written to Latex .tex files and the figures as pdf.
* The estout package is required for the tables

* REFORM INDICATORS
* reform1_7 is the general reform dummy. 0 for non-treated cohorts and 1 for treated cohorts. Missing for all cohorts that are not part of the +/- 7 year reform windows
* There are also reform type-specific reform dummies for extension reforms, detracking reforms and pure detracking reforms. These are definied as the general reform dummy
* with treated and non-treated cohorts but only for each type of reform. They are coded as missing for the other types of reforms and are in this wayused in the if-statements 
* to include/exclude the different forms of reforms
* reform1_eonly_7: reform dummy for extension reforms
* reform1_d_7: reform dummy for detracking reforms
* reform1_donly_7: reform dummy for pure detracking reforms

*set working directory
cd "?"

*Load dataset 
use "Replication_data_ESS1-9_20201113.dta", clear

*Set reference level for reform-dummies
fvset base 1 reform_id_num  yrbrn 

*Control variables macro
global control_var "c.female c.blgetmg_d c.fbrneur c.mbrneur c.fnotbrneur c.mnotbrneur c.agea  c.agea i.essround c.yrbrn c.yrbrn#c.yrbrn c.yrbrn#i.reform_id_num c.agea#i.reform_id_num c.agea#c.agea c.agea#c.agea#i.reform_id_num  i.reform_id_num"

*If-statement for models
 global if_sel_bandwidth "agea>=25 & agea<=80 &  brncntr==1  & reform_years<=7 & reform_years>=-7 " //bandwidth7

***********************************************************************
*Descriptive statistics
***********************************************************************
eststo clear
estpost sum yrbrn agea eduyrs25 ppltrst pplhlp pplfair  trustindex3 paredu_a_high female blgetmg_d fbrneur mbrneur fnotbrneur mnotbrneur reform1_7 reform1_eonly_7 reform1_d_7 reform1_donly_7 ipudrst_rev inst_trustindex5 [aweight=dweight] ///
 if !mi(female) & !mi(agea) & !mi(fbrneur) & !mi(mbrneur) & !mi(fnotbrneur) & !mi(mnotbrneur) & !mi(blgetmg_d) & !mi(yrbrn) & !mi(essround) ///
& agea>=25 & agea<=80 & brncntr==1 & !mi(trustindex3) & !mi(reform1_7) & !mi(reform_id_num) & reform_years<=7 & reform_years>=-7

 esttab using descriptive.tex, replace cells("count mean(fmt(a3))  sd(fmt(a3))  min(fmt(a3)) max(fmt(a3))")  noobs nomtitle nonumber booktabs
 filefilter descriptive.tex descriptiv.tex, replace from("yrbrn") to("Birth year") 
 filefilter descriptiv.tex descriptive.tex, replace from("agea") to("Age") 
 filefilter descriptive.tex descriptiv.tex, replace from("eduyrs25") to("Years of full-time education") 
 filefilter descriptiv.tex descriptive.tex, replace from("ppltrst") to("Most people can be trusted or you can't be too careful") 
 filefilter descriptive.tex descriptiv.tex, replace from("pplhlp") to("Most of the time people helpful or mostly looking out for themselves") 
 filefilter descriptiv.tex descriptive.tex, replace from("pplfair") to("Most people try to take advantage of you, or try to be fair") 
 filefilter descriptive.tex descriptiv.tex, replace from("paredu\BS_a\BS_high") to("High parental education") 
 filefilter descriptiv.tex descriptive.tex, replace from("female") to("Female") 
 filefilter descriptive.tex descriptiv.tex, replace from("mbrneur") to("Foreign-born mother, born within Europe") 
 filefilter descriptiv.tex descriptive.tex, replace from("fbrneur") to("Foreign-born father, born within Europe") 
 filefilter descriptive.tex descriptiv.tex, replace from("mnotbrneur") to("Foreign-born mother, born outside of Europe") 
 filefilter descriptiv.tex descriptive.tex, replace from("fnotbrneur") to("Foreign-born father, born outside of Europe") 
 filefilter descriptive.tex descriptiv.tex, replace from("blgetmg\BS_d") to("Belongs to ethnic minority") 
 filefilter descriptiv.tex descriptive.tex, replace from("reform1\BS_7 ") to("General reform indicator") 
 filefilter descriptive.tex descriptiv.tex, replace from("reform1\BS_eonly\BS_7") to("Reform indicator: extension reforms") 
 filefilter descriptiv.tex descriptive.tex, replace from("reform1\BS_d\BS_7") to("Reform indicator: all detracking reforms") 
 filefilter descriptive.tex descriptiv.tex, replace from("reform1\BS_donly\BS_7") to("Reform indicator: pures<detracking reforms") 
 filefilter descriptiv.tex descriptive.tex, replace from("trustindex3") to("Social trust scale") 
 filefilter descriptive.tex descriptiv.tex, replace from("ipudrst\BS_rev") to("Understand different people") 
 filefilter descriptiv.tex descriptive.tex, replace from("inst\BS_trustindex5") to("Institutional trust index") 
  esttab, cells("count mean sd min max") noobs nomtitle nonumber 

 *alpha for trust items
alpha ppltrst pplhlp pplfair if !mi(female) & !mi(agea) & !mi(fbrneur) & !mi(mbrneur) & !mi(fnotbrneur) & !mi(mnotbrneur) & !mi(blgetmg_d) & !mi(yrbrn) & !mi(essround) ///
& agea>=25 & agea<=80 & brncntr==1 & !mi(trustindex3) & !mi(reform1_7) & !mi(reform_id_num) & reform_years<=7 & reform_years>=-7 
 
 
***********************************************************************
*SOCIAL Trust scale models (Table 3 in the article)
*The upper and lower panel of the table are produced separately
***********************************************************************
*All reforms
 eststo clear
 eststo: ///
 reg  trustindex3 reform1_7   ///
 $control_var  ///
 if $if_sel_bandwidth   [pweight=dweight] , vce(cluster cntry_cohort) 
 estadd local IA_mod "--"
 estadd local no_cntries "16"
 
 eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var   ///
 if $if_sel_bandwidth   [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "No"
  estadd local no_cntries "16"
 
  eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var   ///
 c.paredu_a_high#($control_var) /// 
 if $if_sel_bandwidth    [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "Yes"
  estadd local no_cntries "16"
 
*Extension reforms 
 eststo: ///
 reg  trustindex3 reform1_7   ///
 $control_var  ///
 if $if_sel_bandwidth   & reform1_eonly_7!=. [pweight=dweight], vce(cluster cntry_cohort)   
  estadd local IA_mod "--"
  estadd local no_cntries "11"
 
 eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var   ///
 if $if_sel_bandwidth   & reform1_eonly_7!=.  [pweight=dweight], vce(cluster cntry_cohort)   
  estadd local IA_mod "No"
  estadd local no_cntries "11"
 
  eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var   ///
 c.paredu_a_high#($control_var) /// 
 if $if_sel_bandwidth   & reform1_eonly_7!=. [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "Yes"
  estadd local no_cntries "11"

  *Main table in article, upper panel, output	
	esttab, replace width(\hsize) ///
	mgroups("All reforms" "Extension reforms", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    se  stats(r2_a  no_cntries N IA_mod, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations"  "Flexible interaction")) b(%9.3fc)   compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label nomtitles eqlabels(none) obslast ///
  nonotes   keep(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high   female blgetmg_d) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///  
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
 )    
  *Main table in article, upper panel, tex
    esttab using "trustindex1_7_a_allext.tex", replace width(\hsize) ///
	mgroups("All reforms" "Extension reforms", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    se  stats(r2_a  no_cntries N IA_mod, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations"  "Flexible interaction")) b(%9.3fc) booktabs  compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label nomtitles eqlabels(none) obslast ///
  nonotes   keep(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high  female blgetmg_d) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///  
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   fbrneur "Foreign father, Europe" mbrneur "Foreign mother, Europe" ///
   fnotbrneur "Foreign father, outside Europe" mnotbrneur "Foreign mother, outside Europe" ///
   female "Female" blgetmg_d "Ethnic minority" ///
 )  
   *Appendix table including all control variables
    esttab using "trustindex1_7_a_allext_app.tex", replace width(\hsize) ///
	mgroups("All reforms" "Extension reforms", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
    se  stats(r2_a  no_cntries N IA_mod, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations"  "Flexible interaction")) b(%9.3fc) booktabs  compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label nomtitles eqlabels(none) obslast ///
  nonotes   keep(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high  female blgetmg_d fbrneur mbrneur fnotbrneur mnotbrneur 2.essround 3.essround 4.essround 5.essround 6.essround 7.essround 8.essround 9.essround) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) /// 
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   fbrneur "Foreign-born father, Europe" mbrneur "Foreign-born mother, Europe" ///
   fnotbrneur "Foreign-born father, outside Europe" mnotbrneur "Foreign-born mother, outside Europe" ///
   female "Female" blgetmg_d "Ethnic minority" ///
   2.essround "ESS round 2"  3.essround "ESS round 3"  4.essround "ESS round 4"  5.essround "ESS round 5"  6.essround "ESS round 6"  7.essround "ESS round 7"    8.essround "ESS round 8" 9.essround "ESS round 9" ///
 )  
 
 eststo clear
 *Detracking reforms
  eststo: ///
 reg  trustindex3 reform1_7   ///
 $control_var  ///
 if $if_sel_bandwidth   & reform1_d_7!=. [pweight=dweight] , vce(cluster cntry_cohort)   
  estadd local IA_mod "--"
  estadd local no_cntries "11"
 
 eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high    ///
 $control_var     ///
 if $if_sel_bandwidth   & reform1_d_7!=. [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "No"
  estadd local no_cntries "11"
 margins, dydx(reform1_7) at(paredu_a_high=(0 1)) post
  eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var  ///
 c.paredu_a_high#($control_var) /// 
 if $if_sel_bandwidth   & reform1_d_7!=.    [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "Yes"
  estadd local no_cntries "11"
 
 *Pure detracking reforms
   eststo: ///
 reg  trustindex3 reform1_7   ///
 $control_var  ///
 if $if_sel_bandwidth  & reform1_donly_7!=.    [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "--"
  estadd local no_cntries "3"
 
 eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var  ///
 if $if_sel_bandwidth  & reform1_donly_7!=.   [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "No"
  estadd local no_cntries "3"
 
  eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high    ///
 $control_var   ///
 c.paredu_a_high#($control_var) /// 
 if $if_sel_bandwidth   & reform1_donly_7!=.   [pweight=dweight] , vce(cluster cntry_cohort) 
  estadd local IA_mod "Yes"
  estadd local no_cntries "3"
  
*Main table in article, lower panel, output
          esttab, replace width(\hsize) ///
	mgroups("Detracking reforms" "Pure detracking reforms", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) nonumbers mtitles("(7)" "(8)" "(9)" "(10)" "(11)" "(12)")  ///
    se  stats(r2_a  no_cntries N  IA_mod, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations"  "Flexible interaction")) b(%9.3fc)  compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label  eqlabels(none) obslast ///
  nonotes   keep(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high fbrneur mbrneur fnotbrneur mnotbrneur  female blgetmg_d) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///   
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
  )
  
  *Main table in article, lower panel, tex
     esttab using "trustindex1_7_a_detrack.tex", replace width(\hsize) ///
	mgroups("Detracking reforms" "Pure detracking reforms", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) nonumbers mtitles("(7)" "(8)" "(9)" "(10)" "(11)" "(12)")  ///
    se  stats(r2_a  no_cntries N  IA_mod, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations"  "Flexible interaction")) b(%9.3fc) booktabs  compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label  eqlabels(none) obslast ///
  nonotes   keep(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high  female blgetmg_d) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///  
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   fbrneur "Foreign father, Europe" mbrneur "Foreign mother, Europe" ///
   fnotbrneur "Foreign father, outside Europe" mnotbrneur "Foreign mother, outside Europe" ///
   female "Female" blgetmg_d "Ethnic minority" ///
 )  
   *Appendix table including all control variables
      esttab using "trustindex1_7_a_detrack_app.tex", replace width(\hsize) ///
	mgroups("Detracking reforms" "Pure detracking reforms", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) nonumbers mtitles("(7)" "(8)" "(9)" "(10)" "(11)" "(12)")  ///
    se  stats(r2_a  no_cntries N  IA_mod, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations"  "Flexible interaction")) b(%9.3fc) booktabs  compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label  eqlabels(none) obslast ///
  nonotes   keep(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high fbrneur mbrneur fnotbrneur mnotbrneur  female blgetmg_d 2.essround 3.essround 4.essround 5.essround 6.essround 7.essround 8.essround 9.essround) /// reform_donly_7 .reform_donly_7#c.uppsecno_p c.reform_5#c.uppsecno_p c.reform_9#c.uppsecno_p 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///  c.reform_5#c.uppsecno_p c.reform_9#c.uppsecno_p 
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   fbrneur "Foreign-born father, Europe" mbrneur "Foreign-born mother, Europe" ///
   fnotbrneur "Foreign-born father, outside Europe" mnotbrneur "Foreign-born mother, outside Europe" ///
   female "Female" blgetmg_d "Ethnic minority" ///
   2.essround "ESS round 2"  3.essround "ESS round 3"  4.essround "ESS round 4"  5.essround "ESS round 5"  6.essround "ESS round 6"  7.essround "ESS round 7"    8.essround "ESS round 8" 9.essround "ESS round 9" ///
 )  
   
   *MECHANISMS for detracking reforms
  *********************************************
  eststo clear
global control_var " c.female c.blgetmg_d c.mbrneur c.fbrneur c.fnotbrneur c.mnotbrneur   c.agea  i.essround c.yrbrn c.yrbrn#c.yrbrn c.yrbrn#i.reform_id_num c.agea#i.reform_id_num c.agea#c.agea c.agea#c.agea#i.reform_id_num  i.reform_id_num"
 
 * Base + eduyrs FEs 
 eststo: ///
 reg  trustindex3 reform1_7     ///
 $control_var  i.eduyrs25   ///
 if $if_sel_bandwidth   & reform1_d_7!=. [pweight=dweight], vce(cluster cntry_cohort) 
  estadd local eduyrs_FEs "Yes"
  estadd local sample "Full"
  estadd local no_cntries "10"
  
 * Base + eduyrs FEs  parental education interaction
 eststo: ///
 reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var  i.eduyrs25   ///
 if $if_sel_bandwidth   & reform1_d_7!=. [pweight=dweight], vce(cluster cntry_cohort) 
  estadd local eduyrs_FEs "Yes"
  estadd local sample "Full"
  estadd local no_cntries "10"
  
 *Below or equal 40
 eststo: ///
 reg  trustindex3 reform1_7     ///
 $control_var  ///
 if $if_sel_bandwidth   & reform1_d_7!=. & agea<=40 [pweight=dweight], vce(cluster cntry_cohort) 
 estadd local eduyrs_FEs "No"
 estadd local sample "$\leq$40 yrs"
 estadd local no_cntries "7"
  
*One parent not born in country, ethnic minority or discriminated group
*Need to adjust control macro to not include controls for both foreign-born parents within Europe and outside Europe. However still control for foreign-born from outside Europe. Also exclude ethnic minority control.
global control_var "c.female c.agea  c.fnotbrneur c.mnotbrneur i.essround yrbrn c.yrbrn#c.yrbrn c.yrbrn#i.reform_id_num c.agea#i.reform_id_num c.agea#c.agea c.agea#c.agea#i.reform_id_num  i.reform_id_num" 
eststo: ///
 reg  trustindex3  reform1_7   ///
 $control_var    ///
 if $if_sel_bandwidth  & ((facntr==0 | mocntr==0) | blgetmg_d==1 | dscrgrp==1) & reform1_d_7!=.  [pweight=dweight], vce(cluster cntry_cohort) 
  estadd local eduyrs_FEs "No"
  estadd local sample "Minority"
  estadd local no_cntries "10"
  
*Use standard control variable macro again  
global control_var " c.female c.blgetmg_d c.mbrneur c.fbrneur c.fnotbrneur c.mnotbrneur   c.agea  i.essround c.yrbrn c.yrbrn#c.yrbrn c.yrbrn#i.reform_id_num c.agea#i.reform_id_num c.agea#c.agea c.agea#c.agea#i.reform_id_num  i.reform_id_num"

 *Important to understand other ppl
   eststo: ///
 reg  ipudrst_rev reform1_7    ///
 $control_var   ///
 if $if_sel_bandwidth  & reform1_d_7!=. [pweight=dweight], vce(cluster cntry_cohort) 
 estadd local eduyrs_FEs "No"
 estadd local sample "Full"
 estadd local no_cntries "10"
 
  eststo: ///
 reg  ipudrst_rev reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high     ///
 $control_var   ///
 if $if_sel_bandwidth  & reform1_d_7!=. [pweight=dweight], vce(cluster cntry_cohort) 
 estadd local eduyrs_FEs "No"
 estadd local sample "Full"
 estadd local no_cntries "10"

 * effect on inst trust
  eststo: ///
 reg  inst_trustindex5 reform1_7   ///
 $control_var  ///
 if $if_sel_bandwidth   & reform1_d_7!=. [pweight=dweight], vce(cluster cntry_cohort) 
      estadd local no_cntries "10"
 estadd local eduyrs_FEs "No"
 estadd local sample "Full"
 
   *Main table in article, output
  esttab, replace width(\hsize)  ///
     se  stats(r2_a no_cntries N eduyrs_FEs sample, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations" "Education FEs" "Sample")) b(%9.3fc)   compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label nomtitles eqlabels(none) obslast ///
  nonotes   keep( reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high  female blgetmg_d) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///  
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   eduyrs25 "Years of education" c.eduyrs25#c.eduyrs25 "Years of education$^2$" ///
   female "Female" blgetmg_d "Ethnic minority" ///
   )     
  *Main table in article, tex
  esttab using "trustindex1_7_educont_a.tex", replace width(\hsize)  ///
  mgroups("\shortstack{Social trust:\\education controls}" "\shortstack{Social trust:\\restricted samples}" "\shortstack{Understand\\different people}" "\shortstack{Inst.\\trust}", pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
  booktabs  se  stats(r2_a no_cntries N eduyrs_FEs sample, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations" "Education FEs" "Sample")) b(%9.3fc)   compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label nomtitles eqlabels(none) obslast ///
  nonotes   keep( reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high  female blgetmg_d) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) ///  
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   eduyrs25 "Years of education" c.eduyrs25#c.eduyrs25 "Years of education$^2$" ///
   female "Female" blgetmg_d "Ethnic minority" ///
   )    
 *Appendix table including all control variables  
  esttab using "trustindex1_7_educont_a_app.tex", replace width(\hsize)  ///
  mgroups("\shortstack{Social trust:\\education controls}" "\shortstack{Social trust:\\restricted samples}" "\shortstack{Understand\\different people}" "\shortstack{Inst.\\trust}", pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
  booktabs  se  stats(r2_a no_cntries N eduyrs_FEs sample, fmt(a2) labels("Adj. R$^2$" "Countries" "Observations" "Education FEs" "Sample")) b(%9.3fc)   compress ///
  star(* 0.10 ** 0.05 *** 0.01) unstack label nomtitles eqlabels(none) obslast ///
  nonotes   keep( reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high fbrneur mbrneur fnotbrneur mnotbrneur  female blgetmg_d 2.essround 3.essround 4.essround 5.essround 6.essround 7.essround 8.essround 9.essround) /// 
  order(reform1_7 paredu_a_high c.reform1_7#c.paredu_a_high) /// 
  varlabels(_cons Constant paredu_a_high "High parental edu" agea "Age" ///
   reform1_7 "Reform"  c.reform1_7#c.paredu_a_high "Ref $\times $ High par edu" ///
   fbrneur "Foreign-born father, Europe" mbrneur "Foreign-born mother, Europe" ///
   fnotbrneur "Foreign-born father, outside Europe" mnotbrneur "Foreign-born mother, outside Europe" ///
   female "Female" blgetmg_d "Ethnic minority" ///
   2.essround "ESS round 2"  3.essround "ESS round 3"  4.essround "ESS round 4"  5.essround "ESS round 5"  6.essround "ESS round 6"  7.essround "ESS round 7"    8.essround "ESS round 8" 9.essround "ESS round 9" ///
 )  

 ******************************
 *BANDWIDTH PLOT social trust, first without parental education interaction, then with parental education interaction
 *****************************
 *NO INTERACTION
 *All reforms
 *Loop model: 1 to 7 year window
 set scheme s1mono
 eststo clear
 global control_var " c.female c.blgetmg_d c.mbrneur c.fbrneur c.fnotbrneur c.mnotbrneur   c.agea  i.essround c.yrbrn c.yrbrn#c.yrbrn c.yrbrn#i.reform_id_num c.agea#i.reform_id_num c.agea#c.agea c.agea#c.agea#i.reform_id_num  i.reform_id_num"

forvalues i = 1/7 {
     reg  trustindex3 reform1_7    ///
 $control_var  ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears1 `allyears1'reform_year`i' ||   //first print existing content of allyears1, then add rform_year`i' ||
    local labels1 `labels1' `i'
}
 // check
display "`allyears1'"
display `"`labels1'"'
coefplot `allyears1', keep(reform1_7) vertical bycoefs bylabels(`labels1') yline(0) name(bandw_ppltrst_all, replace) title("All reforms") ciopts(recast(rcap))   ylabel(-0.4(0.2)0.4 ,grid  gmin gmax)  plotregion(style(none)) 

*Extension reforms
*Loop model: 1 to 7 year window
 eststo clear
forvalues i = 1/7 {
     reg  trustindex3 reform1_7    ///
 $control_var   ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform1_eonly_7!=. & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears2 `allyears2'reform_year`i' ||
    local labels2 `labels2' `i'
}
 // check
display "`allyears2'"
display `"`labels2'"'
coefplot `allyears2', keep(reform1_7) vertical bycoefs bylabels(`labels2') yline(0) name(bandw_ppltrst_eduext, replace) title("Extension reforms")  ciopts(recast(rcap)) ylabel(-0.4(0.2)0.4 ,grid  gmin gmax)  plotregion(style(none)) 

*Detracking reforms
*Loop model: 1 to 7 year window
 eststo clear
forvalues i = 1/7 {
     reg  trustindex3 reform1_7    ///
 $control_var  ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform1_d_7!=. & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears3 `allyears3'reform_year`i' ||
    local labels3 `labels3' `i'
}
 // check
display "`allyears3'"
display `"`labels3'"'
coefplot `allyears3', keep(reform1_7) vertical bycoefs bylabels(`labels3') yline(0) name(bandw_ppltrst_detrack, replace) title("Detracking reforms") ciopts(recast(rcap))  ylabel(-0.2(0.2)0.6 ,grid  gmin gmax) plotregion(style(none))

*Pure detracking reforms
*Loop model: 2 to 7 year window (too little data to go down to 1 year window)
 eststo clear
forvalues i = 2/7 {
     reg  trustindex3 reform1_7   ///
 $control_var  ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform1_donly_7!=. & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears4 `allyears4'reform_year`i' ||
    local labels4 `labels4' `i'
}
 // check
display "`allyears4'"
display `"`labels4'"'
coefplot `allyears4', keep(reform1_7) vertical bycoefs bylabels(`labels4') yline(0) name(bandw_ppltrst_puredetrack, replace) title("Pure detracking reforms") ciopts(recast(rcap))   ylabel(-0.2(0.2)0.6 ,grid  gmin gmax) plotregion(style(none))  

*Combine different graphs: all, extension, detrack, pure detrack
gr combine bandw_ppltrst_all bandw_ppltrst_eduext  bandw_ppltrst_detrack bandw_ppltrst_puredetrack,  rows(2) cols(2) ysize(4.5)
gr export bandwidth_plot.pdf, replace

*PARENTAL INTERACTION (not full interaction)
*All reforms
*Loop model: 1 to 7 year window
 eststo clear
 global control_var " c.female c.blgetmg_d c.mbrneur c.fbrneur c.fnotbrneur c.mnotbrneur   c.agea  i.essround c.yrbrn c.yrbrn#c.yrbrn c.yrbrn#i.reform_id_num c.agea#i.reform_id_num c.agea#c.agea c.agea#c.agea#i.reform_id_num  i.reform_id_num"
forvalues i = 1/7 {
     reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var  ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears5 `allyears5'reform_year`i' ||  
    local labels5 `labels5' `i'
}
 // check
display "`allyears5'"
display `"`labels5'"'
coefplot `allyears5', keep(reform1_7) vertical bycoefs bylabels(`labels5') yline(0) name(bandw_ppltrst_all_nofia, replace) title("All reforms")  ciopts(recast(rcap)) ylabel(-0.4(0.2)0.4 ,grid  gmin gmax)  plotregion(style(none)) 

 *Bandwidth plot social trust, edu extension,  NOT full interaction model, 
*Loop model 1 to 7 year window
 eststo clear
forvalues i = 1/7 {
     reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var   ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform1_eonly_7!=. & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears6 `allyears6'reform_year`i' ||
    local labels6 `labels6' `i'
}
 // check
display "`allyears6'"
display `"`labels6'"'
coefplot `allyears6', keep(reform1_7) vertical bycoefs bylabels(`labels6') yline(0) name(bandw_ppltrst_eduext_nofia, replace) title("Extension reforms") ciopts(recast(rcap)) ylabel(-0.4(0.2)0.4 ,grid  gmin gmax)  plotregion(style(none)) 

*Bandwidth plot detracking on  social trust, NOT full interaction model, 
*Loop model 
 eststo clear
forvalues i = 1/7 {
     reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var  ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform1_d_7!=. & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears7 `allyears7'reform_year`i' ||
    local labels7 `labels7' `i'
}
 // check
display "`allyears7'"
display `"`labels7'"'
coefplot `allyears7', keep(reform1_7) vertical bycoefs bylabels(`labels7') yline(0) name(bandw_ppltrst_detrack_nofia, replace) title("Detracking reforms") ciopts(recast(rcap))  ylabel(-0.2(0.2)0.6 ,grid  gmin gmax) plotregion(style(none))

*Bandwidth plot pure detracking on  social trust, NOT full interaction model, 
*Loop model 
 eststo clear
forvalues i = 2/7 {
     reg  trustindex3 reform1_7  paredu_a_high c.reform1_7#c.paredu_a_high   ///
 $control_var  ///
 if agea>=25 & agea<=80 &  brncntr==1 & reform1_donly_7!=. & reform_years<=`i' & reform_years>=-`i' [pweight=dweight], vce(cluster cntry_cohort)
    estimates store reform_year`i'
    local allyears8 `allyears8'reform_year`i' ||
    local labels8 `labels8' `i'
}
 // check
display "`allyears8'"
display `"`labels8'"'
coefplot `allyears8', keep(reform1_7) vertical bycoefs bylabels(`labels8') yline(0) name(bandw_ppltrst_puredetrack_nofia, replace) title("Pure detracking reforms") ciopts(recast(rcap))  ylabel(-0.2(0.2)0.6 ,grid  gmin gmax) plotregion(style(none))

*Combine different graphs: all, extension, detrack, pure detrack
gr combine bandw_ppltrst_all_nofia bandw_ppltrst_eduext_nofia  bandw_ppltrst_detrack_nofia bandw_ppltrst_puredetrack_nofia,  rows(2) cols(2) ysize(4.5)
gr export bandwidth_plot_paria.pdf, replace


  